package com.kuaigou.cim.action.api;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.farsunset.cim.nio.mutual.Message;
import com.farsunset.ichat.cim.push.DefaultMessagePusher;
import com.farsunset.ichat.cim.push.SystemMessagePusher;
import com.farsunset.ichat.common.util.Constants;
import com.farsunset.ichat.common.util.ContextHolder;

public class MessageAction {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
    public  void send(Message message) throws Exception {
		message.setMapStr(message.getMapStr());		
		try{	        
			checkParams(message);	        
			if(Constants.KG_SYSTEM.equals(message.getSender())){
				logger.info("SystemMessagePusher" + message.toString());
				  //向客户端 发送消息
		        ContextHolder.getBean(SystemMessagePusher.class).pushMessageToUser(message);
			}else{
				logger.info("DefaultMessagePusher" + message.toString());
				  //向客户端 发送消息
		        ContextHolder.getBean(DefaultMessagePusher.class).pushMessageToUser(message);
			}	              
	      
		}catch(Exception e){			
			logger.error("推送消息失败：" + e.getMessage() + "." + message.toString());
		}
	 
    }
	
	
	
    /**
     * 文件由客户端发往阿里云 OSS 存储
     * @param messageServiceImpl
     * @throws Exception 
     */
   /* private void fileHandler(Message mo, HttpServletRequest request) throws IOException 
    {
    	if(request instanceof MultiPartRequestWrapper){
			MultiPartRequestWrapper pr = (MultiPartRequestWrapper) request;
			if(pr.getFiles("file")!=null)
			{
				File file = pr.getFiles("file")[0];
		         
		        String fileType = request.getParameter("fileType");
		        String dir = dirMap.get(fileType);
		        if(StringUtils.isEmpty(dir))
		        {
		          	  throw new IllegalArgumentException("fileType:" +fileType+" 未定义" );
		          	  
		        }
		        	String path = request.getSession().getServletContext().getRealPath(dir);
		        	String uuid=UUID.randomUUID().toString().replaceAll("-", "");
		 		    File des = new File(path+"/"+uuid);
		 		    FileUtil.copyFile(file, des);
		 		    mo.setFile(dir+"/"+uuid);
		 		    mo.setFileType(fileType);
			}
        }
          
    }*/
    
    
    private void checkParams(Message message) throws Exception    {    	   
          if(StringUtils.isEmpty(message.getReceiver())) {
        	  throw new RuntimeException("receiver 不能为空!");        	  
          }
//          String account = message.getReceiver();
//          String verificationCode = message.getVerificationCode();	         
//          if(verificationCode == null || account == null ||  !account.equals(AESEncodeUtil.decrypt(verificationCode,AESEncodeUtil.key))){
//        	  throw new RuntimeException("校验码不正确!");        
//          }			
          
    }
 

}
